package com.codingvip.str;

public class LongestCommonPrefix {
    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        String prefix = strs[0];
        for (int i = 1; i < strs.length; i++) {
            System.out.println("第"+i);
            if (prefix == null || prefix.length() == 0) {
                break;
            }
            //没有匹配到的话
            if (strs[i].indexOf(prefix) !=0) {
                prefix = strs[0].substring(0, prefix.length() - 1);
                System.out.println("重置i=1");
                i = 0;

            }
        }
        return prefix;
    }

    public static void main(String[] args) {
       String[] strs=new String[] {"flower", "flow", "flight"};
//       String[] strs2=new String[] {"dog","racecar","car"};
       String[] strs2=new String[] {"dog",""};
        LongestCommonPrefix longestCommonPrefix = new LongestCommonPrefix();
        System.out.println(longestCommonPrefix.longestCommonPrefix(strs));
        System.out.println(longestCommonPrefix.longestCommonPrefix(strs2));

    }
}
